package com.the_ring.chapter06;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.the_ring.chapter06.bean.GoodsInfo;
import com.the_ring.chapter06.databse.DatabaseHelper;

import java.util.List;

public class ShoppingChannelActivity extends AppCompatActivity {

    private ImageView backIv;
    private TextView titleTv;
    private ImageView cartIv;
    private TextView cartCountTv;
    private GridLayout channelGl;

    private DatabaseHelper helper;

    private int goodsInCart = 0;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_channel);

        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        helper = DatabaseHelper.getInstance(this);
        helper.openWriteLink();

        initData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        helper.closeLink();
    }

    public void initView() {
        backIv = findViewById(R.id.iv_back);
        titleTv = findViewById(R.id.tv_title);
        cartIv = findViewById(R.id.iv_shop);
        cartCountTv = findViewById(R.id.tv_count);
        channelGl = findViewById(R.id.gl_channel);

        backIv.setOnClickListener(v -> {
            finish();
        });

        cartIv.setOnClickListener(v -> {
            Intent intent = new Intent(ShoppingChannelActivity.this, ShoppingCartActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        });
    }

    public void initData() {
        titleTv.setText("手机商场");
        goodsInCart = helper.countOfCart();
        cartCountTv.setText(String.valueOf(goodsInCart));

        showGoods();
    }

    @SuppressLint("MissingInflatedId")
    private void showGoods() {
        channelGl.removeAllViews();

        List<GoodsInfo> goodsInfoList = helper.queryAllGoods();
        for (GoodsInfo goodsInfo : goodsInfoList) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_goods, null);
            TextView nameTv = view.findViewById(R.id.tv_name);
            ImageView thumbIv = view.findViewById(R.id.iv_thumb);
            TextView priceTv = view.findViewById(R.id.tv_price);
            Button addBt = view.findViewById(R.id.bt_add);

            nameTv.setText(goodsInfo.name);
            thumbIv.setImageURI(Uri.parse(goodsInfo.picPath));
            thumbIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ShoppingChannelActivity.this, ShoppingDetailActivity.class);
                    intent.putExtra("goods_id", goodsInfo.rowId);
                    startActivity(intent);
                }
            });
            priceTv.setText("" + (int) goodsInfo.price);
            addBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addToCart(goodsInfo.rowId);
                }
            });
            channelGl.addView(view);
        }
    }

    private void addToCart(long rowId) {
        helper.insertToCart(rowId);
        goodsInCart++;
        cartCountTv.setText(String.valueOf(goodsInCart));
    }
}